<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Euclidean distance between polyhedra in 2D</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch08_geometric_probs/html/eucl_dist_poly_2D.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Euclidean distance between polyhedra in 2D</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.2.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/09/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% Given two polyhedra C = {x | A1*x &lt;= b1} and D = {x | A2*x &lt;= b2}, the</span>
<span class="comment">% distance between them is the optimal value of the problem:</span>
<span class="comment">%           minimize    || x - y ||_2</span>
<span class="comment">%               s.t.    A1*x &lt;= b1</span>
<span class="comment">%                       A2*y &lt;= b2</span>
<span class="comment">% Note: here x is in R^2</span>

<span class="comment">% Input data</span>
randn(<span class="string">'seed'</span>,0);
n = 2;
m = 2*n;
A1 = randn(m,n);
b1 = randn(m,1);
A2 = randn(m,n);
b2 = randn(m,1);

fprintf(1,<span class="string">'Computing the distance between the 2 polyhedra...'</span>);
<span class="comment">% Solution via CVX</span>
cvx_begin
    variables <span class="string">x(n)</span> <span class="string">y(n)</span>
    minimize (norm(x - y))
    norm(x,1) &lt;= 2;
    norm(y-[4;3],inf) &lt;=1;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------'</span>);
disp(<span class="string">'The distance between the 2 polyhedra C and D is: '</span> );
disp([<span class="string">'dist(C,D) = '</span> num2str(cvx_optval)]);
disp(<span class="string">'The optimal points are: '</span>)
disp(<span class="string">'x = '</span>); disp(x);
disp(<span class="string">'y = '</span>); disp(y);

<span class="comment">%Plotting</span>
figure;
fill([-2; 0; 2; 0],[0;2;0;-2],<span class="string">'b'</span>, [3;5;5;3],[2;2;4;4],<span class="string">'r'</span>)
axis([-3 6 -3 6])
axis <span class="string">square</span>
hold <span class="string">on</span>;
plot(x(1),x(2),<span class="string">'k.'</span>)
plot(y(1),y(2),<span class="string">'k.'</span>)
plot([x(1) y(1)],[x(2) y(2)])
title(<span class="string">'Euclidean distance between 2 polyhedron in R^2'</span>);
xlabel(<span class="string">'x_1'</span>);
ylabel(<span class="string">'x_2'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the distance between the 2 polyhedra... 
Calling Mosek 9.1.9: 15 variables, 5 equality constraints
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 5               
  Cones                  : 5               
  Scalar variables       : 15              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 5               
  Cones                  : 5               
  Scalar variables       : 15              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 3
Optimizer  - Cones                  : 3
Optimizer  - Scalar variables       : 10                conic                  : 7               
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 6                 after factor           : 6               
Factor     - dense dim.             : 0                 flops                  : 5.40e+01        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   4.0e+00  1.0e+00  2.0e+00  0.00e+00   1.000000000e+00   0.000000000e+00   1.0e+00  0.00  
1   1.6e+00  3.9e-01  7.4e-01  -7.69e-02  1.745839661e+00   1.452430446e+00   3.9e-01  0.02  
2   4.4e-01  1.1e-01  9.0e-02  9.65e-01   2.438967963e+00   2.285065173e+00   1.1e-01  0.02  
3   4.9e-02  1.2e-02  3.6e-03  9.07e-01   2.134713080e+00   2.118964344e+00   1.2e-02  0.02  
4   3.7e-04  9.4e-05  2.4e-06  9.88e-01   2.121484330e+00   2.121364392e+00   9.4e-05  0.02  
5   2.2e-05  5.5e-06  3.4e-08  1.00e+00   2.121333459e+00   2.121326455e+00   5.5e-06  0.02  
6   1.1e-06  2.7e-07  3.6e-10  1.00e+00   2.121321075e+00   2.121320737e+00   2.7e-07  0.02  
7   2.1e-08  5.2e-09  1.0e-12  1.00e+00   2.121320359e+00   2.121320352e+00   5.2e-09  0.02  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 2.1213203591e+00    nrm: 4e+00    Viol.  con: 8e-13    var: 0e+00    cones: 1e-08  
  Dual.    obj: 2.1213203524e+00    nrm: 1e+00    Viol.  con: 0e+00    var: 1e-12    cones: 4e-09  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 7         time: 0.02    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +2.12132
 
Done! 
------------------------------------------------------------------
The distance between the 2 polyhedra C and D is: 
dist(C,D) = 2.1213
The optimal points are: 
x = 
    1.5000
    0.5000

y = 
    3.0000
    2.0000

</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="eucl_dist_poly_2D__01.png" alt=""> 
</div>
</div>
</body>
</html>